package com.example.administrator.taxicar.ui.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.example.administrator.taxicar.R;
import com.example.administrator.taxicar.ui.fragment.AdressFragment;
import com.example.administrator.taxicar.ui.fragment.BaiduMapFragment;
import com.example.administrator.taxicar.ui.fragment.MenuFragment;
import com.example.administrator.taxicar.utils.Constants;
import com.warmtel.slidingmenu.lib.SlidingMenu;
import com.warmtel.slidingmenu.lib.app.SlidingActivity;


public class MainActivity extends SlidingActivity implements MenuFragment.OnFragmentInterfaceListener {
    SlidingMenu slidingMenu;
    String path;
    //双击退出的判定
    private long exitTime = 0;
    private ClearBroadcastReceiver myBroadcastReceiver;
    private FragmentTransaction ft;
    private MenuFragment menuFragment;
    private AdressFragment adressFragment;
    private LocalBroadcastManager mBroadcastManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
        setContentView(R.layout.activity_main_layout);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main_titlebar_layout);//自定义布局赋

        findViewById(R.id.title_left_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });
        getSupportFragmentManager().beginTransaction().add(R.id.mainactivity_layout, BaiduMapFragment.newInstance()).commit();

        ft = getSupportFragmentManager() .beginTransaction();
        menuFragment=MenuFragment.newInstance();


        adressFragment=AdressFragment.newInstance();
        ft.add(R.id.adress_layout, adressFragment).commit();
        setBehindContentView(R.layout.sliding_menu_fragment);
        getSupportFragmentManager().beginTransaction().add(R.id.sliding_fragment,menuFragment ).commit();

        //给广播注册  关闭adressFragment
        mBroadcastManager = LocalBroadcastManager.getInstance(this);
        myBroadcastReceiver = new ClearBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.ADRESSFRAGMENT_BRODCAST);
        mBroadcastManager.registerReceiver(myBroadcastReceiver, intentFilter);

        slidingMenu = getSlidingMenu();
        // 设置滑动菜单视图的宽度
        slidingMenu.setBehindOffsetRes(R.dimen.sliding_menu_offset);
        //从左边滑动
        slidingMenu.setMode(SlidingMenu.LEFT);
        // 设置渐入渐出效果的值
        slidingMenu.setFadeDegree(0.35f);
        //边缘触摸滑动
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
    }

    @Override
    public void toXingcheng() {
        Intent intent = new Intent(this, XingchengActivity.class);
        startActivity(intent);
    }

    @Override
    public void toQianbao() {
        Intent intent = new Intent(this, QianbaoActivity.class);
        startActivity(intent);
    }

    @Override
    public void toXiaoxi() {
        Intent intent = new Intent(this, XiaoxiActivity.class);
        startActivity(intent);
    }

    @Override
    public void toKefu() {
        Intent intent = new Intent(this, KefuAcivvity.class);
        startActivity(intent);
    }

    @Override
    public void toShezhi() {
        Intent intent = new Intent(this, ShezhiActivity.class);
        startActivity(intent);
    }

    @Override
    public void toFankui() {
        Intent intent = new Intent(this, FankuiActivity.class);
        startActivity(intent);
    }

    @Override
    public void toTouxiang() {
        SharedPreferences sharedPreferences = getSharedPreferences(Constants.LOGIN, this.MODE_PRIVATE);
        String phone = sharedPreferences.getString(Constants.USER_LOGIN, "");
        if (phone.equals("请登录")) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(this, TouxiangActivity.class);
            startActivityForResult(intent, Constants.TOUXIANG_ACTIVITY);
        }
    }

    @Override
    public void toLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    public void toPingjia() {
        Intent intent=new Intent(this,PingjiaActivity.class);
        startActivity(intent);
    }

    //双击退出
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (!slidingMenu.isMenuShowing() && keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else if (slidingMenu.isMenuShowing()) {
                toggle();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.TOUXIANG_ACTIVITY) {
            path = data.getExtras().getString("path");
            Log.e("tag", "onActivityResult+path:" + path);
            if (path != null) {
                SharedPreferences mySharedPreferences = getSharedPreferences("feil_picture", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = mySharedPreferences.edit();
                editor.putString("path", path);
                editor.commit();
            }
        }
    }

    public class ClearBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("tag","收到广播");
        }
    }
}